package com.wanmait.hotelManage.servlet;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.wanmait.hotelManage.dao.BreakfastDAO;
import com.wanmait.hotelManage.dao.CarDAO;
import com.wanmait.hotelManage.dao.OrderDAO;
import com.wanmait.hotelManage.dao.UserinfoDAO;
import com.wanmait.hotelManage.dao.VipDAO;
import com.wanmait.hotelManage.vo.Order;
import com.wanmait.hotelManage.vo.Pager;
import com.wanmait.hotelManage.vo.Userinfo;

/**
 * Servlet implementation class UserinfoManageServlet
 */
@WebServlet("/manage/UserinfoManageServlet")
public class UserinfoManageServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public UserinfoManageServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String action=request.getParameter("action");
		if("init".equals(action)) {
			this.init(request, response);
		}else if("OrderInformation".equals(action)) {
			this.OrderInformation(request, response);
		}else if("inittwo".equals(action)) {
			this.inittwo(request, response);
		}else if("OrderInformationTwo".equals(action)) {
			this.OrderInformationTwo(request, response);
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}
	protected void OrderInformationTwo(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		Integer no=Integer.parseInt(request.getParameter("pageNo"));
		Integer id=Integer.parseInt(request.getParameter("id"));
		Pager pager=new Pager();
		pager.setPageNo(no);
		pager.setPageSize(10);
		int rows=OrderDAO.getOrderdao().getrows(id);
		pager.setPageCount(rows);
		List<Order>orders=OrderDAO.getOrderdao().findall(id,pager);
		List<Integer>or=OrderDAO.getOrderdao().findTimeStamp(id);
		Double car=CarDAO.getCarDAO().findPrice();
		int vip=UserinfoDAO.getUserinfodao().findvip(id);
		Double pricevip=VipDAO.getVipDao().findprice(vip);
		Double breakfast=BreakfastDAO.getBreakfastdao().findPrice();
		List<Double>pricess=new ArrayList<Double>();
		for(int i=0;i<orders.size();i++) {
			Order order=orders.get(i);
			Integer price=or.get(i);
			Double prices=price*order.getRoomType().getDiscountPrice();
			if(order.getBreakfast()!=false) {
				prices+=price*breakfast;
			}
			if(order.getCar()!=false) {
				prices+=car;
			}
			prices=prices*pricevip;
			pricess.add(prices);
		}
		request.setAttribute("pricess", pricess);
		request.setAttribute("id", id);
		request.setAttribute("pager", pager);
		request.setAttribute("orders", orders);
		request.getRequestDispatcher("/manage/userinfo/OrderInformationTwo.jsp").forward(request, response);
	}
	/**
	 * 分页
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	protected void inittwo(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		Integer no=Integer.parseInt(request.getParameter("pageNo"));
		Pager pager=new Pager();
		pager.setPageNo(no);
		pager.setPageSize(10);
		int rows=UserinfoDAO.getUserinfodao().getrows();
		pager.setPageCount(rows);
		List<Userinfo>userinfos=UserinfoDAO.getUserinfodao().findAll(pager);
		request.setAttribute("userinfos", userinfos);
		request.setAttribute("pager", pager);
		request.getRequestDispatcher("/manage/userinfo/listTwo.jsp").forward(request, response);
	}
	/**
	 * 订单详情信息
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	protected void OrderInformation(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		Integer id=Integer.parseInt(request.getParameter("id"));
		Pager pager=new Pager();
		pager.setPageNo(1);
		pager.setPageSize(10);
		int rows=OrderDAO.getOrderdao().getrows(id);
		pager.setPageCount(rows);
		List<Order>orders=OrderDAO.getOrderdao().findall(id,pager);
		List<Integer>or=OrderDAO.getOrderdao().findTimeStamp(id);
		int vip=UserinfoDAO.getUserinfodao().findvip(id);
		Double pricevip=VipDAO.getVipDao().findprice(vip);
		Double car=CarDAO.getCarDAO().findPrice();
		Double breakfast=BreakfastDAO.getBreakfastdao().findPrice();
		List<Double>pricess=new ArrayList<Double>();
		for(int i=0;i<orders.size();i++) {
			Order order=orders.get(i);
			Integer price=or.get(i);
			Double prices=price*order.getRoomType().getDiscountPrice();
			if(order.getBreakfast()!=false) {
				prices+=price*breakfast;
			}
			if(order.getCar()!=false) {
				prices+=car;
			}
			prices=prices*pricevip;
			pricess.add(prices);
		}
		request.setAttribute("pricess", pricess);
		request.setAttribute("pager", pager);
		request.setAttribute("orders", orders);
		request.setAttribute("id", id);
		request.getRequestDispatcher("/manage/userinfo/OrderInformation.jsp").forward(request, response);
	}
	/**
	 * 初始化
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	protected void init(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		Pager pager=new Pager();
		pager.setPageNo(1);
		pager.setPageSize(10);
		int rows=UserinfoDAO.getUserinfodao().getrows();
		pager.setPageCount(rows);
		List<Userinfo>userinfos=UserinfoDAO.getUserinfodao().findAll(pager);
		request.setAttribute("userinfos", userinfos);
		request.setAttribute("pager", pager);
		request.getRequestDispatcher("/manage/userinfo/list.jsp").forward(request, response);
	}
}
